package cn.pzhuweb.gpa.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtils {
    private static String driver=null;
    private static String url=null;
    private static String user=null;
    private static String pwd=null;
    private static String key=null;

    static{

        /**
         * 这里的账号密码都是经过En类加密的，请设置key之后把账号密码重新附上
         */
        driver = "com.mysql.cj.jdbc.Driver";
        url = "jdbc:mysql://127.0.0.1:3306/gpa?useSSL=true&characterEncoding=utf-8&rewriteBatchedStatements=true";
        key = "z2x4c5a21232z1WD4a356as";//加密的秘钥
        user = En.decryptStr(key,"aRBp1wP13rL/aYwV5tVg==");//加密过后的账号
        pwd = En.decryptStr(key,"e11BFSf123EkwG1JhlsXSg==");//加密过后的密码
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user, pwd);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static void close(ResultSet res,Statement sta,Connection con){
        if (res!=null) {
            try {
                res.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (sta!=null) {
            try {
                sta.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con!=null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void close(Statement sta,Connection con){
        close(null, sta, con);
    }

}
